package com.example.aixiangshou.runninggame;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * 点击icon后跳转到此页面。点击后将数据回调回去，并刷新。
 *
 */
public class PopuActivity extends AppCompatActivity {
    public static String ICON_DATA = "icon_data";

    PageGridView pageGridView;
    MyPageIndicator pageIndicator;
    int width;
    int height;
    private MyAdapter myAdapter;
    List<IconBean> iconBeanList;
    Activity mAddOption;
    PopuItemListener itemListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popu);


        pageGridView = (PageGridView) findViewById(R.id.pagingGridView);
        pageIndicator= (MyPageIndicator) findViewById(R.id.pageindicator);
        width = getResources().getDisplayMetrics().widthPixels / 2 - 30;
        height = getResources().getDisplayMetrics().widthPixels / 3;

        itemListener = AddOptionActivity.getItemListener();

        iconBeanList = new ArrayList<>();
        iconBeanList.add(new IconBean(R.drawable.icon_01));
        iconBeanList.add(new IconBean(R.drawable.icon_02));
        iconBeanList.add(new IconBean(R.drawable.icon_03));
        iconBeanList.add(new IconBean(R.drawable.icon_04));
        iconBeanList.add(new IconBean(R.drawable.icon_05));
        iconBeanList.add(new IconBean(R.drawable.icon_06));
        iconBeanList.add(new IconBean(R.drawable.icon_07));
        iconBeanList.add(new IconBean(R.drawable.icon_08));
        iconBeanList.add(new IconBean(R.drawable.icon_09));
        iconBeanList.add(new IconBean(R.drawable.icon_10));
        iconBeanList.add(new IconBean(R.drawable.icon_11));
        iconBeanList.add(new IconBean(R.drawable.icon_12));
        iconBeanList.add(new IconBean(R.drawable.icon_13));
        iconBeanList.add(new IconBean(R.drawable.icon_14));
        iconBeanList.add(new IconBean(R.drawable.icon_15));
        iconBeanList.add(new IconBean(R.drawable.icon_16));
        iconBeanList.add(new IconBean(R.drawable.icon_17));
        iconBeanList.add(new IconBean(R.drawable.icon_18));
        iconBeanList.add(new IconBean(R.drawable.icon_19));
        iconBeanList.add(new IconBean(R.drawable.icon_20));
        iconBeanList.add(new IconBean(R.drawable.icon_21));
        iconBeanList.add(new IconBean(R.drawable.icon_22));
        iconBeanList.add(new IconBean(R.drawable.icon_23));
        iconBeanList.add(new IconBean(R.drawable.icon_24));
        iconBeanList.add(new IconBean(R.drawable.icon_25));
        iconBeanList.add(new IconBean(R.drawable.icon_26));
        iconBeanList.add(new IconBean(R.drawable.icon_27));
        iconBeanList.add(new IconBean(R.drawable.icon_28));
        iconBeanList.add(new IconBean(R.drawable.icon_29));
        iconBeanList.add(new IconBean(R.drawable.icon_30));
        iconBeanList.add(new IconBean(R.drawable.icon_31));

        myAdapter = new MyAdapter(iconBeanList, this);
        pageGridView.setAdapter(myAdapter);
        pageGridView.setOnItemClickListener(myAdapter);
        pageGridView.setPageIndicator(pageIndicator);
    }

    class MyAdapter extends PageGridView.PagingAdapter<MyVH> implements PageGridView.OnItemClickListener {
        List<IconBean> mData = new ArrayList<>();
        Activity mActivity;

        public MyAdapter(List<IconBean> data, Activity activity) {
            this.mData.addAll(data);
            mActivity = activity;
        }

        @Override
        public MyVH onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(PopuActivity.this).inflate(R.layout.item_icon, parent, false);
            ViewGroup.LayoutParams params = view.getLayoutParams();
            params.height = height;
            params.width = width;
            view.setLayoutParams(params);
            return new MyVH(view);
        }

        @Override
        public void onBindViewHolder(MyVH holder, int position) {
            if (mData.get(position) instanceof IconBean){
                IconBean bean = mData.get(position);
                if (bean != null) {
                    holder.icon.setImageResource(bean.getIconRec());
                }
            }
        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        @Override
        public List getData() {
            return mData;
        }

        @Override
        public Object getEmpty() {
            return "";
        }

        @Override
        public void onItemClick(PageGridView pageGridView, int position) {
//        String gridview = "";
//        if (pageGridView == pageGridView) {
//            gridview = "第一个GridView";
//        }
//        if (pageGridView == pageGridView2) {
//            gridview = "第二个GridView";
//        }
//        if (pageGridView == pageGridView3) {
//            gridview = "第三个GridView";
//        }
//
//        Toast.makeText(MainActivity.this, gridview + " 第" + (position + 1) + "个item 被点击" + " 值：" + mData.get(position), Toast.LENGTH_SHORT).show();
            itemListener.onItemClick(mData.get(position));
            mActivity.finish();
        }
    }

    class MyVH extends RecyclerView.ViewHolder {
        public ImageView icon;
        public MyVH(View itemView) {
            super(itemView);
            icon= (ImageView) itemView.findViewById(R.id.imv_icon);
        }
    }
}
